﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QuickEncoder
{
    public partial class frmEncoder : Form
    {
        public enum EncodingAlgorithm
        {
            Nothing,
            Base64
        }

        public frmEncoder()
        {
            InitializeComponent();
            LoadEncodingMethods();
        }

        private void LoadEncodingMethods()
        {
            string[] strEncodingAlgorithms = Enum.GetNames(typeof (EncodingAlgorithm));
            foreach (string strEA in strEncodingAlgorithms)
            {
                if (!cmbAlgorithm.Items.Contains(strEA) && strEA != EncodingAlgorithm.Nothing.ToString())
                {
                    cmbAlgorithm.Items.Add(strEA);
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            stsBarLabelStatus.Text = "Hello World! CR-38. Waiting for Command!";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (cmbAlgorithm.SelectedItem != null)
            {
                string strOutputString = string.Empty;

                if (cmbAlgorithm.SelectedItem.ToString() == EncodingAlgorithm.Base64.ToString())
                {
                    if (radEncode.Checked)
                    {
                        ASCIIEncoding encoding = new ASCIIEncoding();
                        byte[] bytes = encoding.GetBytes(txtStringToEncodeDecode.Text);
                        string sBase64 = Convert.ToBase64String(bytes);

                        strOutputString = sBase64;
                    }
                    else if (radDecode.Checked)
                    {
                        byte[] bytes = null;

                        try
                        {
                            bytes = Convert.FromBase64String(txtStringToEncodeDecode.Text);
                        }
                        catch (Exception ConvertFromBase64StringException)
                        {
                            bytes = null;
                            stsBarLabelStatus.Text = "Bad Input String";
                            stsBarLabelStatus.ToolTipText = ConvertFromBase64StringException.Message;
                        }

                        if (bytes != null)
                        {
                            ASCIIEncoding encoding = new ASCIIEncoding();

                            try
                            {
                                strOutputString = encoding.GetString(bytes, 0, bytes.Length);
                            }
                            catch (Exception GetStringException)
                            {
                                stsBarLabelStatus.Text = "Bad Input String";
                                stsBarLabelStatus.ToolTipText = GetStringException.Message;
                                strOutputString = string.Empty;
                            }
                        }
                    }

                    if (strOutputString.Length > 0)
                    {
                        txtOutput.Text = strOutputString;
                        txtOutput.Visible = true;
                        if (chkCopyOutputToClipboard.Checked)
                        {
                            Clipboard.SetData(DataFormats.Text, strOutputString);
                            stsBarLabelStatus.Text = "Conversion done and the output available in system clipboard.";
                        }
                        else
                        {
                            stsBarLabelStatus.Text = "Conversion done.";
                        }
                    }                    
                }
            }
        }

        private void cmbAlgorithm_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void chkCopyOutputToClipboard_CheckedChanged(object sender, EventArgs e)
        {

        }
    }
}
